package com.kstech.ks_gaz01.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankii.androidlibrary.utils.CalcUtil
import com.kstech.ks_gaz01.data.DebugDataBean
import com.kstech.ks_gaz01.databinding.FragmentZtProgressBinding

import com.kstech.ks_gaz01.realTime.ChildFragment
import com.kstech.ks_gaz01.socket.SocketManagerNew

class ZtProgressFragment : ChildFragment() {
    private var _binding: FragmentZtProgressBinding? = null

    // 只能在onCreateView与onDestroyView之间的生命周期里使用
    private val binding: FragmentZtProgressBinding get() = _binding!!
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        _binding = FragmentZtProgressBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initData()
    }

    private fun initData() {
//        val readBeanList = ArrayList<SocketManagerNew.ReadBean>()
//        DebugDataBean.dataList_ztProgress.forEach {
//            readBeanList.add(SocketManagerNew.ReadBean(it))
//        }
        SocketManagerNew.addReadyLooperList(
            true, arrayListOf(
                SocketManagerNew.ReadBean(
                    DebugDataBean.dataList_ztProgress[0]
                ), SocketManagerNew.ReadBean(
                    DebugDataBean.dataList_ztProgress[1]
                ), SocketManagerNew.ReadBean(
                    DebugDataBean.dataList_ztProgress[2]
                ), SocketManagerNew.ReadBean(
                    DebugDataBean.dataList_ztProgress[3]
                )
            )
        )
    }

    private fun initView() {
    }

    override fun writeSuccess(address: String, value: String) {

    }

    override fun readSuccess(address: String, result: ArrayList<String>) {
        val value = CalcUtil.Hex2IntStr(result[0])
        when (address) {
            //左前
            DebugDataBean.dataList_ztProgress[0] -> {
//                "左前：$value".logd("blankii")
                binding.zq.updateValue(value.toInt())
            }
            //左后
            DebugDataBean.dataList_ztProgress[1] -> {
//                "左后：$value".logd("blankii")
                binding.zh.updateValue(value.toInt())
            }
            //右前
            DebugDataBean.dataList_ztProgress[2] -> {
//                "右前：$value".logd("blankii")
                binding.yq.updateValue(value.toInt())
            }
            //右后
            DebugDataBean.dataList_ztProgress[3] -> {
//                "右后：$value".logd("blankii")
                binding.yh.updateValue(value.toInt())
            }
        }
    }

    override fun pageOnPause() {
        super.pageOnPause()
    }

    override fun pageOnResume() {
        super.pageOnResume()
    }

}